﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using QLTuyenXe.DAL;
using QLTuyenXe.DAO;

namespace QLTuyenXe.BLL
{
    class BUser
    {
        MyConnectUnit myConnU = new MyConnectUnit();
        //Đọc dữ liệu từ bảng
        public ArrayList Select(String columns, String condition, String order)
        {
            ArrayList a = new ArrayList();
            DataTable tb = new DataTable();
            tb = myConnU.Select("NGUOI_DUNG", columns, condition, order);
            if (tb != null)
            {
                foreach (DataRow r in tb.Rows)
                {
                    USER obj = new USER();
                    obj.MaND= r["name"].ToString();
                    obj.HoTen = r["hoten"].ToString();
                    //obj.Password = r["password"].ToString();
                    obj.MaNhom= r["maNhom"].ToString();
                    a.Add(obj);
                }
            }
            return a;
        }
        //Đọc dữ liệu từ bảng Doanh Thu trong CSDL
        public ArrayList Select(String columns, String condition)
        {
            return this.Select(columns, condition, null);
        }

        public ArrayList Select(String columns)
        {
            return this.Select(columns, null);
        }

        public ArrayList Select()
        {
            return this.Select("*");
        }

        public DataTable Select_Nhom()
        {
            DataTable tb = myConnU.Select("NHOM");

            return tb;
        }

        // kiem tra ten dang nhap
        public DataTable Select_TenDN(string tenDN)
        {
            DataTable tb = myConnU.Selectid("NGUOI_DUNG", tenDN);
            return tb;
        }

        //Thêm 1 chức vụ mới
        public int Insert(USER user)
        {
            ColumnData id = new ColumnData("maND", 1, user.MaND.ToString());
            ColumnData name = new ColumnData("name", 1, user.Name.ToString());
            ColumnData hoTen = new ColumnData("hoten", 1, user.HoTen.ToString());
            ColumnData password = new ColumnData("password", 1, user.Password.ToString());
            ColumnData group_id = new ColumnData("maNhom", 1, user.MaNhom.ToString());
            ArrayList ob = new ArrayList();
            ob.Add(id);
            ob.Add(name);
            ob.Add(hoTen);
            ob.Add(password);
            ob.Add(group_id);
            return myConnU.Insert("NGUOI_DUNG", ob);
        }
        //Them vao 1 danh sach
        public int Insert(ArrayList f)
        {
            int count = 0;
            foreach (USER cv in f)
            {
                count += this.Insert(cv);
            }
            return count;
        }
        //Cap nhat thong tin cho 1 dt
        public int Update(USER user, String condition)
        {
            ColumnData name = new ColumnData("name", 2, user.Name.ToString());
            ColumnData hoTen = new ColumnData("hoten", 2, user.HoTen.ToString());
            ColumnData password = new ColumnData("password", 1, user.Password.ToString());
            ColumnData group_id = new ColumnData("maNhom", 2, user.MaNhom.ToString());
            ArrayList ob = new ArrayList();
            ob.Add(name);
            ob.Add(hoTen);
            ob.Add(password);
            ob.Add(group_id);
            return myConnU.Update("NGUOI_DUNG", ob, condition);
        }
        
        // cap nhat ten ho ten, nhóm k tahy pass
        public int Update_DoiNhom(USER user, String condition)
        {
            ColumnData name = new ColumnData("name", 1, user.Name.ToString());
            ColumnData hoTen = new ColumnData("hoten", 1, user.HoTen.ToString());
            ColumnData group_id = new ColumnData("maNhom", 1, user.MaNhom.ToString());
            ArrayList ob = new ArrayList();
            ob.Add(name);
            ob.Add(hoTen);
            ob.Add(group_id);
            return myConnU.Update("NGUOI_DUNG", ob, condition);
        }
        //Dùng cho doi mat khau
        public int Update_MK(USER user, String condition)
        {
            ColumnData password = new ColumnData("password", 1, user.Password.ToString());
            ArrayList ob = new ArrayList();
            ob.Add(password);
            return myConnU.Update("NGUOI_DUNG", ob, condition);
        }



        //Cap nhat 1 danh sach
        public int Update(ArrayList ks, String condition)
        {
            int count = 0;
            foreach (USER a in ks)
            {
                count += this.Update(a, condition);
            }
            return count;
        }
        //Xoa chuc vu theo dieu kien
        public int Delete(String condition)
        {
            return myConnU.Delete("NGUOI_DUNG", condition);
        }

        //load len forn voi 2 bang
        public ArrayList Select_User(string dk)
        {
            ArrayList a = new ArrayList();
            DataTable tb = new DataTable();
            tb = myConnU.Select("NGUOI_DUNG","NHOM","NGUOI_DUNG.maNhom","NHOM.maNhom", dk);
            if (tb != null)
            {
                foreach (DataRow r in tb.Rows)
                {
                    USER obj = new USER();
                    obj.MaND = r["maND"].ToString();
                    obj.HoTen = r["hoten"].ToString();
                    //obj.Password = r["password"].ToString();
                    obj.Name = r["name"].ToString();
                    obj.TenNhom = r["tennhom"].ToString();
                    a.Add(obj);
                }
            }
            return a;
        }


        //Lay ma lon nhat
    //=============================================================================================
        //Hàm lấy giá trị lơn nhất (Mr.Tuyên K_54HTTT)
        public String getOrder()
        {
            string strID = myConnU.GetTopDataCell("NGUOI_DUNG", "maND");
            string strID1;
            string strID2;
                strID1 = strID.Substring(0, 2);
                strID2 = strID.Substring(2, 5);
                int autoNum = Convert.ToInt32(strID2) + 1;

                if (autoNum < 10)
                    return strID1 + "0000" + autoNum;

                else if (autoNum >= 10 && autoNum < 100)
                    return strID1 + "000" + autoNum;

                else if (autoNum >= 100 && autoNum < 1000)
                    return strID1 + "00" + autoNum;
                else if (autoNum >= 1000 && autoNum < 10000)
                    return strID1 + "0" + autoNum;
                else if (autoNum >= 10000 && autoNum < 100000)
                    return strID1 + "" + autoNum;

                else
                    return "";
        }
      //End hàm lấy giá trị lưn nhất (Mr.Tuyên K_54HTTT)


        //    hàm cho dang nhap
        public DataTable Select_DN(String condition1, string condition2)
        {
            //ArrayList ar = new ArrayList();
            DataTable tb= myConnU.SelectDN("NGUOI_DUNG", condition1, condition2);
            return tb;
        }
        //Cái nay của form main để kiểm tra quyền
        public DataTable Select_Quyen(String condition1, string condition2)
        {
            //ArrayList ar = new ArrayList();
            DataTable tb = myConnU.SelectQuyen("NGUOI_DUNG","NHOM","NGUOI_DUNG.maNhom", "NHOM.maNhom", condition1, condition2);
            return tb;
        }
    }
}
